package cn.itsource.controller;

import cn.itsource.enums.YesOrNo;
import cn.itsource.pojo.Carousel;
import cn.itsource.pojo.Category;
import cn.itsource.pojo.vo.CategoryVo;
import cn.itsource.pojo.vo.NewItemVo;
import cn.itsource.service.CarouselService;
import cn.itsource.service.CategoryService;
import cn.itsource.utils.JSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(value = "商城首页" ,tags = {"用于商城首页的相关接口"})
@RestController
@RequestMapping("index")
public class IndexController {

    @Autowired
    CarouselService carouselService;

    @Autowired
    CategoryService categoryService;

    @ApiOperation(value = "获取首页轮播图列表",notes = "获取首页轮播图列表",httpMethod = "GET")
    @RequestMapping("/carousel")
    public JSONResult carousel(){
        List<Carousel> carousels = carouselService.queryAll(YesOrNo.YES.type);
        return  JSONResult.ok(carousels);
    }

    @ApiOperation(value = "查询顶级商品分类",notes = "查询顶级商品分类",httpMethod = "GET")
    @RequestMapping("/cats")
    public JSONResult cats(){
        List<Category> rootCategory = categoryService.queryAllRootLevelCat();
        return  JSONResult.ok(rootCategory);
    }

    @ApiOperation(value = "获取商品子分类",notes = "获取商品子分类",httpMethod = "GET")
    @RequestMapping("/subCat/{rootCatId}")
    public JSONResult subCat(
            @ApiParam(name = "rootCatId", value = "一级商品id",required = true)
            @PathVariable Integer rootCatId){

        if (rootCatId == null) {
            return JSONResult.errorMsg("分类不存在");
        }

        List<CategoryVo> rootCategory = categoryService.querySubCatList(rootCatId);
        return  JSONResult.ok(rootCategory);
    }

    @ApiOperation(value = "推荐最新商品",notes = "最新商品推荐",httpMethod = "GET")
    @RequestMapping("/sixNewItems/{rootCatId}")
    public JSONResult sixNewItems(
            @ApiParam(name = "rootCatId", value = "一级商品id",required = true)
            @PathVariable Integer rootCatId){

        if (rootCatId == null) {
            return JSONResult.errorMsg("分类不存在");
        }

        List<NewItemVo> newItemVos = categoryService.querySixNewItemsLazy(rootCatId);
        return  JSONResult.ok(newItemVos);
    }
}
